<template>
  <view class="container">
    <view class="search-section">
      <view class="search-box">
        <image class="search-icon" src="/static/category/search.png" mode="aspectFit" />
        <input class="search-input" type="text" placeholder="搜索石刻..." v-model="searchText" />
      </view>
    </view>

    <view class="category-section">
      <view class="category-tabs">
        <view 
          class="category-tab" 
          :class="{ active: activeDimension === 'author' }"
          @tap="toggleDimension('author')"
        >
          <text>按作者</text>
          <text class="arrow">{{ expandedDimension === 'author' ? '▲' : '▼' }}</text>
        </view>
        <view 
          class="category-tab" 
          :class="{ active: activeDimension === 'dynasty' }"
          @tap="toggleDimension('dynasty')"
        >
          <text>按年代</text>
          <text class="arrow">{{ expandedDimension === 'dynasty' ? '▲' : '▼' }}</text>
        </view>
        <view 
          class="category-tab" 
          :class="{ active: activeDimension === 'content' }"
          @tap="toggleDimension('content')"
        >
          <text>按内容</text>
          <text class="arrow">{{ expandedDimension === 'content' ? '▲' : '▼' }}</text>
        </view>
        <view 
          class="category-tab" 
          :class="{ active: activeDimension === 'style' }"
          @tap="toggleDimension('style')"
        >
          <text>按体例</text>
          <text class="arrow">{{ expandedDimension === 'style' ? '▲' : '▼' }}</text>
        </view>
        <view 
          class="category-tab" 
          :class="{ active: activeDimension === 'location' }"
          @tap="toggleDimension('location')"
        >
          <text>按景点</text>
          <text class="arrow">{{ expandedDimension === 'location' ? '▲' : '▼' }}</text>
        </view>
      </view>

      <view class="sub-categories" v-if="expandedDimension">
        <!-- 所有分类都使用横向滚动 -->
        <scroll-view 
          class="sub-category-scroll"
          scroll-x
          :show-scrollbar="false"
          :enable-flex="true"
        >
          <view class="sub-category-list horizontal">
            <view 
              v-for="item in currentSubCategories" 
              :key="item.value"
              class="sub-category-item"
              :class="{ active: activeSubCategory === item.value }"
              @tap="selectSubCategory(item.value)"
            >
              <text>{{ item.label }}</text>
            </view>
          </view>
        </scroll-view>
      </view>
    </view>

    <view class="filter-tag" v-if="activeSubCategory">
      <text class="filter-label">当前筛选：</text>
      <text class="filter-value">{{ getCurrentFilterLabel() }}</text>
      <text class="clear-filter" @tap="clearFilter">✕</text>
    </view>

    <scroll-view class="main-content" scroll-y>

      <view class="artworks-section">
        <view class="artwork-card" @tap="viewArtwork('zhuxitiqu')">
          <view class="artwork-image">
            <text class="image-placeholder">朱熹题刻图片</text>
          </view>
          <view class="artwork-info">
            <view class="dynasty-tag song">宋代</view>
            <text class="artwork-title">朱熹题刻</text>
            <text class="artwork-desc">"逝者如斯夫，不舍昼夜"</text>
            <text class="artwork-location">天游峰 · 宋淳熙十年</text>
          </view>
          <view class="artwork-action">
            <text class="action-text">详情</text>
          </view>
        </view>

        <view class="artwork-card" @tap="viewArtwork('wangxizhi')">
          <view class="artwork-image">
            <text class="image-placeholder">王羲之石刻图片</text>
          </view>
          <view class="artwork-info">
            <view class="dynasty-tag ming">明代</view>
            <text class="artwork-title">王羲之石刻</text>
            <text class="artwork-desc">"兰亭集序"节选</text>
            <text class="artwork-location">九曲溪 · 明万历年间</text>
          </view>
          <view class="artwork-action">
            <text class="action-text">详情</text>
          </view>
        </view>

        <view class="artwork-card" @tap="viewArtwork('dongpo')">
          <view class="artwork-image">
            <text class="image-placeholder">苏东坡石刻图片</text>
          </view>
          <view class="artwork-info">
            <view class="dynasty-tag song">宋代</view>
            <text class="artwork-title">苏东坡题刻</text>
            <text class="artwork-desc">"水调歌头"诗句</text>
            <text class="artwork-location">一线天 · 宋元丰年间</text>
          </view>
          <view class="artwork-action">
            <text class="action-text">详情</text>
          </view>
        </view>

        <view class="artwork-card" @tap="viewArtwork('kangxi')">
          <view class="artwork-image">
            <text class="image-placeholder">康熙御笔图片</text>
          </view>
          <view class="artwork-info">
            <view class="dynasty-tag qing">清代</view>
            <text class="artwork-title">康熙御笔</text>
            <text class="artwork-desc">"武夷精舍"四字</text>
            <text class="artwork-location">大王峰 · 清康熙年间</text>
          </view>
          <view class="artwork-action">
            <text class="action-text">详情</text>
          </view>
        </view>
      </view>
    </scroll-view>
  </view>
</template>

<script setup>
import { ref, computed } from 'vue'

const searchText = ref('')
const activeDimension = ref('')
const expandedDimension = ref('')
const activeSubCategory = ref('')

// 五大分类维度的小分类数据
const categories = {
  author: [
    { label: '全部', value: 'all' },
    { label: '朱熹', value: 'zhushi' },
    { label: '王阳明', value: 'wangyangming' },
    { label: '苏东坡', value: 'sudongpo' },
    { label: '陆游', value: 'luyou' },
    { label: '康熙', value: 'kangxi' }
  ],
  dynasty: [
    { label: '全部', value: 'all' },
    { label: '宋代', value: 'song' },
    { label: '元代', value: 'yuan' },
    { label: '明代', value: 'ming' },
    { label: '清代', value: 'qing' },
    { label: '近现代', value: 'modern' }
  ],
  content: [
    { label: '全部', value: 'all' },
    { label: '题诗', value: 'poetry' },
    { label: '记事', value: 'record' },
    { label: '题名', value: 'signature' },
    { label: '游记', value: 'travel' },
    { label: '题字', value: 'inscription' }
  ],
  style: [
    { label: '全部', value: 'all' },
    { label: '楷书', value: 'kaishu' },
    { label: '行书', value: 'xingshu' },
    { label: '草书', value: 'caoshu' },
    { label: '篆书', value: 'zhuanshu' },
    { label: '隶书', value: 'lishu' }
  ],
  location: [
    { label: '全部', value: 'all' },
    { label: '天游峰', value: 'tianyoufeng' },
    { label: '九曲溪', value: 'jiuquxi' },
    { label: '一线天', value: 'yixiantian' },
    { label: '虎啸岩', value: 'huxiaoyan' },
    { label: '大红袍', value: 'dahongpao' }
  ]
}

// 当前展开维度的小分类列表
const currentSubCategories = computed(() => {
  return expandedDimension.value ? categories[expandedDimension.value] : []
})

// 切换大分类维度
const toggleDimension = (dimension) => {
  if (expandedDimension.value === dimension) {
    expandedDimension.value = ''
  } else {
    expandedDimension.value = dimension
    activeDimension.value = dimension
  }
}

// 选择小分类
const selectSubCategory = (value) => {
  activeSubCategory.value = value
  console.log('选择分类:', activeDimension.value, value)
  // 选中后隐藏小分类列表
  expandedDimension.value = ''
  // TODO: 这里可以根据选中的分类筛选石刻数据
}

// 获取当前筛选条件的显示文本
const getCurrentFilterLabel = () => {
  if (!activeSubCategory.value || !activeDimension.value) return ''
  const categoryList = categories[activeDimension.value]
  const item = categoryList.find(c => c.value === activeSubCategory.value)
  return item ? item.label : ''
}

// 清除筛选条件
const clearFilter = () => {
  activeSubCategory.value = ''
  activeDimension.value = ''
}

const viewArtwork = (artworkId) => {
  uni.navigateTo({
    url: '/pages/detail/detail'
  })
}
</script>

<style scoped>
.container {
  width: 100vw;
  height: 100vh;
  background: linear-gradient(180deg, #e8f5e8 0%, #c8e6c9 50%, #81c784 100%);
  color: #2e7d32;
  box-sizing: border-box;
  display: flex;
  flex-direction: column;
}

.search-section {
  padding: 12px 16px;
  background: linear-gradient(180deg, #e8f5e8 0%, #c8e6c9 100%);
}

.search-box {
  display: flex;
  align-items: center;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 24px;
  padding: 10px 16px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.search-icon {
  width: 20px;
  height: 20px;
  margin-right: 12px;
  flex-shrink: 0;
}

.search-input {
  flex: 1;
  font-size: 14px;
  color: #333333;
  border: none;
  outline: none;
  background: transparent;
}

.category-section {
  padding: 0 16px 12px 16px;
  background: linear-gradient(180deg, #c8e6c9 0%, #e8f5e8 100%);
}

.main-content {
  flex: 1;
  width: 100%;
  padding: 0 16px;
  box-sizing: border-box;
  height: 100%;
  overflow-y: auto;
}

.category-tabs {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 8px;
  display: flex;
  gap: 6px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.category-tab {
  flex: 1;
  padding: 10px 6px;
  border-radius: 12px;
  text-align: center;
  font-size: 13px;
  color: #2e7d32;
  font-weight: 500;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  transition: all 0.3s ease;
}

.category-tab.active {
  background: #4caf50;
  color: white;
}

.category-tab .arrow {
  font-size: 10px;
  color: inherit;
}

.sub-categories {
  margin-top: 12px;
  background: rgba(255, 255, 255, 0.95);
  border-radius: 12px;
  padding: 12px;
  box-shadow: 0 4px 16px rgba(0, 0, 0, 0.08);
  animation: slideDown 0.3s ease;
}

@keyframes slideDown {
  from {
    opacity: 0;
    transform: translateY(-10px);
  }
  to {
    opacity: 1;
    transform: translateY(0);
  }
}

.sub-category-scroll {
  width: 100%;
  white-space: nowrap;
  overflow: hidden;
}

.sub-category-list {
  display: inline-flex;
  flex-wrap: nowrap;
  white-space: nowrap;
  padding-right: 16px;
  min-width: 100%;
  gap: 8px;
}

.sub-category-item {
  padding: 8px 16px;
  border-radius: 20px;
  background: #f1f8e9;
  color: #2e7d32;
  font-size: 13px;
  font-weight: 500;
  transition: all 0.3s ease;
  flex-shrink: 0;
  white-space: nowrap;
}

.sub-category-item.active {
  background: #4caf50;
  color: white;
  box-shadow: 0 2px 8px rgba(76, 175, 80, 0.3);
}

.filter-tag {
  margin: 12px 16px;
  padding: 10px 16px;
  background: rgba(76, 175, 80, 0.1);
  border-radius: 24px;
  display: flex;
  align-items: center;
  gap: 8px;
  border: 1px solid rgba(76, 175, 80, 0.3);
}

.filter-label {
  font-size: 13px;
  color: #2e7d32;
  font-weight: 500;
}

.filter-value {
  flex: 1;
  font-size: 13px;
  color: #4caf50;
  font-weight: bold;
}

.clear-filter {
  width: 20px;
  height: 20px;
  border-radius: 10px;
  background: rgba(76, 175, 80, 0.2);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #2e7d32;
}

.artworks-section {
  margin-bottom: 100px;
}

.artwork-card {
  background: rgba(255, 255, 255, 0.95);
  border-radius: 16px;
  padding: 16px;
  margin-bottom: 16px;
  display: flex;
  align-items: center;
  gap: 16px;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.1);
}

.artwork-image {
  width: 80px;
  height: 80px;
  background: #f1f8e9;
  border-radius: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.image-placeholder {
  font-size: 12px;
  color: #81c784;
  text-align: center;
}

.artwork-info {
  flex: 1;
}

.dynasty-tag {
  padding: 4px 12px;
  border-radius: 12px;
  font-size: 12px;
  font-weight: bold;
  display: inline-block;
  margin-bottom: 8px;
  color: white;
}

.dynasty-tag.song { background: #ff9800; }
.dynasty-tag.ming { background: #2196f3; }
.dynasty-tag.qing { background: #e91e63; }

.artwork-title {
  display: block;
  font-size: 16px;
  font-weight: bold;
  color: #1b5e20;
  margin-bottom: 4px;
}

.artwork-desc {
  display: block;
  font-size: 14px;
  color: #4caf50;
  margin-bottom: 4px;
}

.artwork-location {
  font-size: 12px;
  color: #81c784;
}

.artwork-action {
  flex-shrink: 0;
}

.action-text {
  background: #4caf50;
  color: white;
  padding: 8px 16px;
  border-radius: 16px;
  font-size: 12px;
  font-weight: bold;
}
</style>
